//
//  DYZTabbarController.m
//  DYZCars
//
//  Created by MF on 16/8/18.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZTabbarController.h"
#import "DYZNavigationController.h"

@interface DYZTabbarController ()

@end

@implementation DYZTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIViewController *sourceVCVC = [self loadViewControllerFromNsstring:@"DYZCarSourceController" withTabBarTitle:@"车源" withControllerColor:[UIColor darkGrayColor] andTabbarImage:@"cy_xc"];
    [self addChildViewController:sourceVCVC];
        UIViewController *findVC = [self loadViewControllerFromNsstring:@"DYZFindController" withTabBarTitle:@"寻车" withControllerColor:[UIColor whiteColor] andTabbarImage:@"cy_xc"];
    [self addChildViewController:findVC];
    UIViewController *mineVC = [self loadViewControllerFromStoryboardName:@"mine" withTabBarTitle:@"我的" withControllerColor:[UIColor whiteColor] andTabbarImage:@"cy_xc"];
    [self addChildViewController:mineVC];

}

//加载控制器的两种方式
// 1，从storyBoard中加载箭头指定的控制器
- (UIViewController *)loadViewControllerFromStoryboardName:(NSString *)storyboardName withTabBarTitle:(NSString *)title withControllerColor:(UIColor *)color andTabbarImage:(NSString *)imageName {
    
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
    UIViewController *vc =  [sb instantiateInitialViewController];
    return  [self loadViewController:vc withTabBarTitle:title withControllerColor:color andTabbarImage:imageName];
}

// 1.2，从storyBoard中加载非箭头指定的控制器
- (UIViewController *)loadViewControllerFromStoryboardName:(NSString *)storyboardName andstoryboardIdentfier:(NSString *)identfier withTabBarTitle:(NSString *)title withControllerColor:(UIColor *)color andTabbarImage:(NSString *)imageName {
    
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
    //    加载控制器中非箭头指定的控制器，需要传进来要加载的控制器的ID
    UIViewController *vc =  [sb instantiateViewControllerWithIdentifier:identfier];
    return  [self loadViewController:vc withTabBarTitle:title withControllerColor:color andTabbarImage:imageName];
}

// 2，通过字符串进行加载控制器
- (UIViewController *)loadViewControllerFromNsstring:(NSString *)className withTabBarTitle:(NSString *)title withControllerColor:(UIColor *)color andTabbarImage:(NSString *)imageName{
    
    Class viewC = NSClassFromString(className);
    
    UIViewController *vc = [[viewC alloc] init];
    
    return   [self loadViewController:vc withTabBarTitle:title withControllerColor:color andTabbarImage:imageName];
    
    
}

- (UIViewController *)loadViewController:(UIViewController *)vc withTabBarTitle:(NSString *)title withControllerColor:(UIColor *)color andTabbarImage:(NSString *)imageName {
    
    vc.title = title;
    
    vc.view.backgroundColor = color;
    
    vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //    自定义UINavigationController 来修改属性
    DYZNavigationController *nav = [[DYZNavigationController alloc] initWithRootViewController:vc];
    return nav;
}
@end
